﻿using FairyGUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using WgFrame.Net;

public class TavernPanel : BasePopupPanel
{
    private GRichTextField preview;
    /// <summary>
    /// 招募一次消耗钱
    /// </summary>
    private GTextField tfZhaoMuOneGold;
    /// <summary>
    /// 招募十次消耗钱
    /// </summary>
    private GTextField tfZhaoMuTenGold;
    /// <summary>
    /// 招募多少次收必出橙色
    /// </summary>
    private GTextField tfZhaoMuOneCount;
    private GTextField tfZhaoMuTime;


    public override void Init()
    {
        base.Init();

        frame = view.GetChild("n1").asCom;
        preview = frame.GetChild("n27").asRichTextField;
        tfZhaoMuOneCount = frame.GetChild("n17").asTextField;
        tfZhaoMuOneGold = frame.GetChild("n19").asTextField;
        tfZhaoMuTenGold = frame.GetChild("n24").asTextField;
        tfZhaoMuTime = frame.GetChild("n20").asTextField;
        preview.onClick.Add(OnPreview);
        frame.GetChild("n18").asButton.onClick.Add(OnZhaoMuOne);
        frame.GetChild("n25").asButton.onClick.Add(OnZhaoMuTen);
    }

    public override void Show(params object[] para)
    {
        MsgListenerMgr.AddMsgListener(MsgListenerType.UpdateZhaoMuTime, OnUpdateZhaoMuTime);
        MsgListenerMgr.AddMsgListener(MsgListenerType.ZhaoMuOk, OnZhaoMuOk);
        base.Show(para);

        if (!InitDataMgr.TryGet(out InitData initData))
        {
            return;
        }

        if (GameNetwork.Player.BaseInfo.ZhaoMuRemainTime > 0)
        {
            tfZhaoMuTime.visible = true;
            tfZhaoMuOneGold.visible = true;
        }
        else
        {
            tfZhaoMuTime.visible = false;
            tfZhaoMuOneGold.visible = false;
        }

        tfZhaoMuOneGold.SetVar("count", initData.recruit_rmb_once.ToString());
        tfZhaoMuOneGold.FlushVars();
        tfZhaoMuOneCount.SetVar("count", GameNetwork.Player.BaseInfo.ZhaoMuNeedCount.ToString());
        tfZhaoMuOneCount.FlushVars();
        tfZhaoMuTenGold.SetVar("count", initData.recruit_rmb_ten.ToString());
        tfZhaoMuOneGold.FlushVars();
        OnUpdateZhaoMuTime(para);
    }

    public override void Close()
    {
        MsgListenerMgr.RemoveMsgListener(MsgListenerType.UpdateZhaoMuTime, OnUpdateZhaoMuTime);
        MsgListenerMgr.RemoveMsgListener(MsgListenerType.ZhaoMuOk, OnZhaoMuOk);
        base.Close();
    }

    protected override GComponent CreateView()
    {
        return UIPackage.CreateObject("Main", "panel_tavern1").asCom;
    }

    private void OnPreview(EventContext context)
    {
        PopupPanelManager.Open<TavernPreviewPanel>();
    }

    private void OnUpdateZhaoMuTime(object[] para)
    {
        if (GameNetwork.Player.BaseInfo.ZhaoMuRemainTime > 0)
        {
            tfZhaoMuOneGold.visible = true;
            tfZhaoMuTime.visible = true;
            int h = (int)(GameNetwork.Player.BaseInfo.ZhaoMuRemainTime / 3600);
            int s = (int)(GameNetwork.Player.BaseInfo.ZhaoMuRemainTime % 3600 / 60);
            int m = (int)(GameNetwork.Player.BaseInfo.ZhaoMuRemainTime % 60);
            string time = string.Format("{0:D2}:{1:D2}:{2:D2}", h, s, m);
            tfZhaoMuTime.SetVar("time", time);
            tfZhaoMuTime.FlushVars();
        }
        else
        {
            tfZhaoMuTime.visible = false;
            tfZhaoMuOneGold.visible = false;
        }
    }

    private void OnZhaoMuOne(EventContext context)
    {
        if (!InitDataMgr.TryGet(out InitData initData))
        {
            return;
        }

        if (GameNetwork.Player.BaseInfo.ZhaoMuRemainTime > 0 && GameNetwork.Player.BaseInfo.Rmb < initData.recruit_rmb_once)
        {
            FlyTextPanelMgr.Show("仙缘不足");
            return;
        }

        RequestHelper.SendZhaoMuRequest(1);
    }

    private void OnZhaoMuTen(EventContext context)
    {
        if (!InitDataMgr.TryGet(out InitData initData))
        {
            return;
        }

        if (GameNetwork.Player.BaseInfo.Rmb < initData.recruit_rmb_ten)
        {
            FlyTextPanelMgr.Show("仙缘不足");
            return;
        }

        RequestHelper.SendZhaoMuRequest(2);
    }

    private void OnZhaoMuOk(object[] para)
    {
        tfZhaoMuOneCount.SetVar("count", GameNetwork.Player.BaseInfo.ZhaoMuNeedCount.ToString());
        tfZhaoMuOneCount.FlushVars();
        PopupPanelManager.Open<ZhaoMuResultPanel>(para);
    }
}
